package com.tongcao.cn.mq.to;

import com.aliyun.openservices.ons.api.Message;
import com.tongcao.cn.util.HessianUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @Author:tongcao
 * @Date:2019/1/25 17:11
 * Description:
 */
public class AsyncMsgTO implements AsyncMsg {

    private static final long serialVersionUID = 1L;

    private Logger log = LoggerFactory.getLogger(AsyncMsgTO.class);

    private Message message;

    public AsyncMsgTO(Message message) {
        this.message = message;
    }

    /**
     * 取消息体，已经反序列化
     *
     * @return
     */
    @Override
    public <T> T getContent() {
        try {
            return (T) HessianUtil.deserialize(message.getBody());
        } catch (IOException e) {
            log.error("AsyncMsgTO deserialize error!", e);
            return null;
        }
    }

    @Override
    public Message getMessage() {
        return message;
    }

    @Override
    public String getTopic() {
        return message.getTopic();
    }

    @Override
    public String getTag() {
        return message.getTag();
    }

    @Override
    public String getKey() {
        return message.getKey();
    }

    /**
     * 取消息id
     *
     * @return
     */
    @Override
    public String getMsgID() {
        return message.getMsgID();
    }

    /**
     * 取重试次数
     *
     * @return
     */
    @Override
    public int getReconsumeTimes() {
        return message.getReconsumeTimes();
    }

    /**
     * 取开始投递的时间
     *
     * @return
     */
    @Override
    public long getStartDeliverTime() {
        return message.getStartDeliverTime();
    }

    @Override
    public String getOriginMsgID() {
        return getMsgID();
    }

    @Override
    public String toString() {
        return message.toString();
    }
}
